<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        body {
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            background-image: linear-gradient(90deg, #ffafbd, #ffc3a0);
            background-repeat: no-repeat;
        }

        .container {
            padding: 30px 40px 20px;
            text-align: center;
            width: 440px;
            max-width: 100%;
            margin: 0 auto;
            border-radius: 15px;
            background: #f2f2f2;
        }

        .span {
            position: relative;
            display: block;
            font-size: 16.5px;
            padding: 0.34em 0.84em;
            border: 2px solid #494a4b;
            border-radius: inherit;
            background-color: #fff;
        }

        li {
            margin-bottom: 8px;
        }

        .active {
            background: #fe7345 !important;
            color: #fff !important;
        }
    </style>
</head>

<body>

    <div id="app">
        <div class="container">
            <input v-model="val" type="text">

            <span @click="addItem" class="span">提交</span>

            <ul>
                <li :class="{
                    active: item.check
                }" v-for="(item, index) in list" v-show="item.isActive">
                    <input type="checkbox" v-model="item.check">
                    {{ item.title }} -------------- <span @click="delItem(index)">X</span>
                </li>
            </ul>

            <p>
                总数量: {{ allLength }}
            </p>

            <div>
                <span @click="all" v-show="flag">All</span> --- 
                <span  @click="active" v-show="activeFlag">Active</span>
                <span @click="completed" v-show="activeFlag">Completed</span>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>

        new Vue({
            // 元素
            el: "#app",
            // 定义的数据(状态)
            data: {
                val: "",
                // 初始化数组
                list: [],
                flag: true,
                activeFlag: false
            },
            computed: {
                allLength() {
                    const arr = this.list.filter(item => item.check === false)
                    return arr.length
                }
            },
            watch: {
                list: {
                    deep: true,
                    // handler  如果写成箭头函数 this 会指向window
                    handler(newValue){
                        //
                        // Array.every   全部满足条件, 返回值为true
                        //  Array.some(item => ) 只要有一个满足条件, 返回值为true
                        this.activeFlag = newValue.some(item => item.check)
                    }
                }
            },
            methods: {
                addItem() {
                    this.list.push({
                        title: this.val,
                        check: false,
                        isActive: true
                    })
                    this.val = ""
                },
                // 删除任务
                delItem(index) {
                    this.list.splice(index, 1)
                },
                completed() {
                    this.list.forEach(item => {
                        item.isActive = item.check
                    })
                },
                active() {
                    this.list.forEach(item => {
                        item.isActive = !item.check
                    })
                },
                all() {
                    this.list.forEach(item => {
                        item.isActive = true
                    })
                }
            }
        })
    </script>
</body>

</html>